/*****************************************************************************
 *  This file is part of the OpenDomo project.
 *  Copyright(C) 2011 OpenDomo Services SL
 *  
 *  Daniel Lerch Hostalot <dlerch@opendomo.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *****************************************************************************/


#include <upnpdev.h>
#include <upnplog.h>

UpnpDevice_Handle hdl = -1;

// {{{ upnpdev_init()
int upnpdev_init(char *ip_address, unsigned short port, char *desc_doc_name,
   char *web_dir_path, int (*upnpdev_callback) (Upnp_EventType, void*, void*))
{
   int ret=0;
   
   if(!desc_doc_name)
   {
      upnplog_printf(UPNPLOG_ERROR, "[upnpdev], desc_doc_name error %d: " 
         "line %d, file %s\n", ret, __LINE__, __FILE__);
      return -1;
   }

   if(!web_dir_path)
   {
      upnplog_printf(UPNPLOG_ERROR, "[upnpdev], web_dir_path error %d: "
         "line %d, file %s\n", ret, __LINE__, __FILE__);
      return -1;
   }


   /* UPnP SDK initializacion */
   upnplog_printf(UPNPLOG_INFO, "Initializing UPnP Sdk with "
         "ipaddress = %s port = %d\n", ip_address, port);

   if( (ret=UpnpInit(ip_address, port)) != UPNP_E_SUCCESS ) 
   {
      upnplog_printf(UPNPLOG_ERROR, "[upnpdev], UpnpInit() error %d:  "
         "line %d, file %s\n", ret, __LINE__, __FILE__);
      UpnpFinish();

      return -1;
   }
   if(ip_address == NULL) 
      ip_address = UpnpGetServerIpAddress();

   if(port == 0) 
      port = UpnpGetServerPort();




   /* Set WebServer Root Dir */
   char desc_doc_url[512];

   snprintf(desc_doc_url, 512, 
         "http://%s:%d/%s", ip_address, port, desc_doc_name);

   upnplog_printf(UPNPLOG_INFO,"Desc URL: %s\n", desc_doc_url);
   upnplog_printf(UPNPLOG_INFO,"Webserver root directory: %s\n", web_dir_path);

   if( (ret=UpnpSetWebServerRootDir(web_dir_path)) != UPNP_E_SUCCESS ) 
   {
      upnplog_printf(UPNPLOG_ERROR, 
         "[upnpdev], UpnpSetWebServerRootDir() error %d:  "
         "line %d, file %s\n", ret, __LINE__, __FILE__);
      UpnpFinish();

      return -1;
   }

   /* Register Root Device */
   if( (ret=UpnpRegisterRootDevice(desc_doc_url, 
               upnpdev_callback, &hdl, &hdl) ) != UPNP_E_SUCCESS ) 
   {
      upnplog_printf(UPNPLOG_ERROR, 
         "[upnpdev], UpnpRegisterRootDevice() error %d:  "
         "line %d, file %s\n", ret, __LINE__, __FILE__);
      UpnpFinish();
      
      return -1;
   } 

   /* Send Advertistment */
   if( (ret=UpnpSendAdvertisement(hdl, 100) ) != UPNP_E_SUCCESS ) 
   {
      upnplog_printf(UPNPLOG_ERROR, 
         "[upnpdev], UpnpSendAdvertisement() error %d:  "
         "line %d, file %s\n", ret, __LINE__, __FILE__);
      UpnpFinish();
      
      return -1;
   }

   return 0;
}
// }}}

// {{{ upnpdev_cleanup()
int upnpdev_cleanup()
{
   UpnpUnRegisterRootDevice(hdl);
   UpnpFinish();
   
   return 0;
}
// }}}






